package com.cloud.controller;

import com.cloud.entity.Project;
import com.cloud.entity.User;
import com.cloud.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("Project")
public class ProjectController {
    @Autowired
    ProjectService projectService;


    @PostMapping(value = "/add",consumes = "application/json")
    public Map<?,?> addProject(@RequestBody Project project) {
        if(project.getPId()==null){
            Map map = new HashMap();
            map.put("project",projectService.addProject(project));
            return map;
        }
        return null;
    }

    @PostMapping("/del")
    public Map<?,?> deleteProject(@RequestParam Integer pId) {
//        System.out.println("***************************************");
        Map map = new HashMap<>();
        map.put("code",projectService.deleteProject(pId));
        return map;
    }

    @GetMapping("/all")
    public Map<?,?> allProjects() {
        Map map = new HashMap();
        map.put("all",projectService.allProjects());
        return map;
    }

    @GetMapping("/getbyuser_id/{user_id}")
    public Map<?,?> getProjectById(@PathVariable("user_id") Integer user_id){
        Map map = new HashMap();
        map.put("project",projectService.getProjectById(user_id));
        return map;
    }

    @GetMapping("/getbyptitle/{p_title}")
    public Map<?,?> getProjectByTitle(@PathVariable("p_title") String p_title){
        Map map = new HashMap();
        map.put("project",projectService.getProjectByName(p_title));
        return map;
    }

    @PostMapping(value = "/update",consumes = "application/json")
    public Map<?,?> updateProject(@RequestBody Project project){
        System.out.println(project);
        Map map = new HashMap();
        map.put("project",projectService.updateProject(project));
        return map;
    }
}
